package com.zb.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.*;
import com.zb.mapper.ZjGoodsParamMapper;
import com.zb.result.PageUtil;
import com.zb.service.*;
import com.zb.vo.GoodsParamListVo;
import com.zb.vo.GoodsParamSelectVo;
import com.zb.vo.GoodsParamVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品参数表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-08-01
 */
@Service
public class ZjGoodsParamServiceImpl extends ServiceImpl<ZjGoodsParamMapper, ZjGoodsParam> implements ZjGoodsParamService {
    @Resource
    private ZjGoodsParamMapper goodsParamMapper;
    @Resource
    private ZjGoodsParamFieldService goodsParamFieldService;
    @Resource
    private ZjProductParamService productParamService;
    @Resource
    private ZjSpareRelationParamService spareRelationParamService;

    // 新增或修改商品参数
    @Override
    public void save(Long custAccId, GoodsParamVo paramVo) {
        Assert.isTrue(null == goodsParamMapper.existByName(custAccId, paramVo.getId(), paramVo.getName()), "商品参数已重复！", paramVo.getName());
        ZjGoodsParam param = new ZjGoodsParam();
        BeanUtils.copyProperties(paramVo, param);
        param.setDisabled(paramVo.getStatus());
        if (null != param.getId()) {
            this.updateById(param);
            return;
        }
        param.setId(IdWorker.getId());
        param.setCustAccId(custAccId);
        this.save(param);
    }

    // 获取商品参数列表
    @Override
    public Map<String, Object> getList(Long custAccId, GoodsParamSelectVo selectVo) {
        QueryWrapper<ZjGoodsParam> wrapper = new QueryWrapper<>();
        wrapper.eq("cust_acc_id", custAccId);
        wrapper.orderByDesc("gmt_update");
        if (StringUtils.isNotBlank(selectVo.getName())) {
            wrapper.like("name", selectVo.getName());
        }
        PageUtil.setPageLimit(selectVo);
        Page<ZjGoodsParam> page = this.page(new Page<>(selectVo.getPage(), selectVo.getLimit()), wrapper);

        List<GoodsParamListVo> voList = new ArrayList<>();
        page.getRecords().forEach(t -> voList.add(GoodsParamListVo.beanToVo(t)));
        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), voList);
    }

    // 批量禁用或启用商品参数
    @Override
    public void updateBatchDisableByIds(List<Long> idList, boolean status) {
        goodsParamMapper.updateBatchDisableByIds(idList, status);
    }

    // 批量删除商品参数
    @Override
    @Transactional
    public void deleteByIds(List<Long> idList) {
        QueryWrapper<ZjProductParam> productParamQueryWrapper = new QueryWrapper<>();
        productParamQueryWrapper.select("param_id");
        productParamQueryWrapper.in("param_id", idList);
        productParamQueryWrapper.last("limit 10");
        List<ZjProductParam> paramList = productParamService.list(productParamQueryWrapper);
        if (CollectionUtil.isNotEmpty(paramList)) {
            QueryWrapper<ZjGoodsParam> wrapper = new QueryWrapper<>();
            wrapper.select("name");
            wrapper.in("id", paramList.stream().map(ZjProductParam::getParamId).distinct().toList());
            List<ZjGoodsParam> list = this.list(wrapper);
            Assert.isTrue(false, "商品参数[ " + list.stream().map(ZjGoodsParam::getName).collect(Collectors.joining("、")) + " ]已与商品关联使用中，不允许删除");
        }
        QueryWrapper<ZjSpareRelationParam> spareRelationParamQueryWrapper = new QueryWrapper<>();
        spareRelationParamQueryWrapper.select("param_id");
        spareRelationParamQueryWrapper.in("param_id", idList);
        spareRelationParamQueryWrapper.last("limit 10");
        List<ZjSpareRelationParam> spareRelationParamList = spareRelationParamService.list(spareRelationParamQueryWrapper);
        if (CollectionUtil.isNotEmpty(spareRelationParamList)) {
            QueryWrapper<ZjGoodsParam> wrapper = new QueryWrapper<>();
            wrapper.select("name");
            wrapper.in("id", spareRelationParamList.stream().map(ZjSpareRelationParam::getParamId).distinct().toList());
            List<ZjGoodsParam> list = this.list(wrapper);
            Assert.isTrue(false, "商品参数[ " + list.stream().map(ZjGoodsParam::getName).collect(Collectors.joining("、")) + " ]已与商品关联使用中，不允许删除");
        }

        goodsParamFieldService.deleteByParamIds(idList);
        this.removeByIds(idList);
    }

    // 分页获取参数下拉数据列表
    @Override
    public Map<String, Object> pageSelect(Long custAccId, SelectVo selectVo) {
        PageUtil.setSelectPageLimit(selectVo);
        QueryWrapper<ZjGoodsParam> wrapper = new QueryWrapper<>();
        wrapper.select("id", "name");
        wrapper.eq("cust_acc_id", custAccId);
        wrapper.eq("is_disabled", false);
        if (StringUtils.isNotBlank(selectVo.getName())) {
            wrapper.like("name", selectVo.getName());
        }
        PageUtil.setSelectPageLimit(selectVo);
        Page<ZjGoodsParam> page = this.page(new Page<>(selectVo.getPage(), selectVo.getLimit()), wrapper);
        List<SelectResultVo> voList = new ArrayList<>();
        page.getRecords().forEach(t -> voList.add(new SelectResultVo(t.getId(), t.getName())));
        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), voList);
    }
}
